﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BulldogCMS.Data.FilesTree
{
    public class XMLFilesTreeProvider : IFilesTreeProvider
    {
        private XDocument _foldersXdoc = null;
        private List<IPartialFileNode> _partialNodes = null;

        public XMLFilesTreeProvider(XDocument foldersXdoc)
        {
            this._foldersXdoc = foldersXdoc;
        }

        public IEnumerable<IPartialFileNode> PartialNodes
        {
            get {
                if (this._partialNodes == null)
                {
                    this._partialNodes = this.LoadPartialNodes();
                }
                return this._partialNodes;
            }
        }

        private List<IPartialFileNode> LoadPartialNodes()
        {
            List<IPartialFileNode> partialNodes = new List<IPartialFileNode>();
            if (this._foldersXdoc != null && this._foldersXdoc.Root != null && this._foldersXdoc.Root.Element("files") != null)
            {
                foreach (var nodeElement in this._foldersXdoc.Root.Element("files").Elements())
                {
                    XMLPartialFileNode partialNode = new XMLPartialFileNode(nodeElement);
                    partialNodes.Add(partialNode);
                }
            }
            return partialNodes;
        }


    }
}
